package dao;

import entity.FilmInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tool.MyBatisUtil;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class FilmInfoDaoImpl implements  FilmInfoDao {

    public List<FilmInfo> getFilminfos() {
        List<FilmInfo> list=null;
        SqlSessionFactoryBuilder builder=null;
        SqlSessionFactory factory=null;
        SqlSession sqlSession=null;
        //sqlSession--sqlSesssionFactory--sqlSesssionFactoryBuilder
        try{
            //读取mybatis的配置文件
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建sqlSesssionFactoryBuilder
             builder=new SqlSessionFactoryBuilder();
            //创建sqlSesssionFactory
             factory=builder.build(is);
            //创建sqlSession crud
             sqlSession=factory.openSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
             list=sqlSession.getMapper(FilmInfoDao.class).getFilminfos();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilminfosBYCondition(FilmInfo info) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilminfosBYCondition(info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilminfosBYCondition2(String choice) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilminfosBYCondition2(choice);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilminfosBYCondition3(Integer[] typeids) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilminfosBYCondition3(typeids);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilminfosBYCondition4(List typeids) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilminfosBYCondition4(typeids);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getcount() {
        int count=-1;
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            count=sqlSession.getMapper(FilmInfoDao.class).getcount();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByPager(Integer pageIndex, Integer pageSize) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByPager(pageIndex,pageSize);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }


    @Override
    public int getcount2(FilmInfo info) {
        int count=-1;
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            count=sqlSession.getMapper(FilmInfoDao.class).getcount2(info);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfoByPager2(FilmInfo info,Integer pageIndex, Integer pageSize) {
        List<FilmInfo> list=new ArrayList<>();
        try{

            SqlSession sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfoByPager2(info,pageIndex,pageSize);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int delFilm(Integer filmid) {
        int count=-1;
        SqlSession sqlSession=null;
        try{

             sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            count=sqlSession.getMapper(FilmInfoDao.class).delFilm(filmid);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int addFilm(FilmInfo info) {
        int count=-1;
        SqlSession sqlSession=null;
        try{

            sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            count=sqlSession.getMapper(FilmInfoDao.class).addFilm(info);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int updateFilm(FilmInfo info) {
        int count=-1;
        SqlSession sqlSession=null;
        try{

            sqlSession= MyBatisUtil.getSession();
            //dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.films");
            count=sqlSession.getMapper(FilmInfoDao.class).updateFilm(info);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }


}
